/*
 * @Author: your name
 * @Date: 2021-10-05 20:19:34
 * @LastEditTime: 2021-10-05 20:56:56
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \luogu\1.2排序\main.cpp
 */

#include<iostream>
using namespace std;

#define NAME_LENGTH 22
#define STU_NUM 105

typedef struct Sinfo
{
  char name[NAME_LENGTH];
  int year;
  int month;
  int day;
}sinfo;

//排序
void SinfoSort(sinfo stu[],int n);
//判断A和B的生日大小 stu1 > stu2 返回1; = 返回0; < 返回 -1;
int compare(sinfo stu1,sinfo stu2);

int main(){
  sinfo stu[STU_NUM];
  int n;
  cin >> n;
  for(int i=1; i<=n; i++){
    cin >> stu[i].name >> stu[i].year >> stu[i].month >> stu[i].day;
  }
  // cout << "--------------------" << endl;
  //打印输出
  // for(int i=1; i<=n; i++){
  //   cout << stu[i].name << "\t"
  //         << stu[i].year << "\t"
  //         << stu[i].month << "\t"
  //         << stu[i].day << endl;
  // }
  SinfoSort(stu,n);
  // cout << "after order" << endl;
  // for(int i=1; i<=n; i++){
  //   cout << stu[i].name << "\t"
  //         << stu[i].year << "\t"
  //         << stu[i].month << "\t"
  //         << stu[i].day << endl;
  // }
  for(int i=1; i<=n; i++){
    cout << stu[i].name << endl;
  }
  return 0;
}

void SinfoSort(sinfo stu[],int n)
{
  //不考虑效率,直接交换数据,相同情况,后输入的先输出
  int i,j;
  for(i=2; i<=n; i++){
    // cout << stu[i].name << " come" << endl;
    //stu[i]年龄更大
    if(compare(stu[i],stu[i-1])!=-1){
      // cout << stu[i].name << " in" << endl;
      //保留stu[i]
      stu[0] = stu[i];
      //这里判断条件 stu[j] <= stu[0]
      for(j=i-1; compare(stu[j],stu[0])!=1; j--){
        // cout << stu[j].name << " back" << endl;
        stu[j+1] = stu[j];
      }
      // cout << j << endl;
      stu[j+1] = stu[0];
    }
  }
}

int compare(sinfo stu1,sinfo stu2)
{
  //比较年
  if(stu1.year < stu2.year){
    return 1;
  }
  if(stu1.year > stu2.year){
    return -1;
  }
  //同年比较月
  if(stu1.month < stu2.month){
    return 1;
  }
  if(stu1.month > stu2.month){
    return -1;
  }
  //同年同月比较日
  if(stu1.day < stu2.day){
    return 1;
  }
  if(stu1.day > stu2.day){
    return -1;
  }
  //同年同月同日
  return 0;
}
